package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.bean.UserStopBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.StopUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import com.koron.util.ValidationKey;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户停用增加
 * 
 * @author 甄玉晴
 *
 */
public class StopUserAdd implements ServerInterface {

	@Override
	@ValidationKey(clazz = UserStopBean.class, method = "insert")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<Map> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", Map.class);
		try {
			StopUserMapper mapper = factory.getMapper(StopUserMapper.class);
			UserStopBean bean = JsonUtils.objectToPojo(req.getData(), UserStopBean.class);
			
			int ingStop = mapper.userStopIng(bean.getUserNo());
			if (ingStop>0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户编号正在停用中，不能再次停用", void.class);
			}
			if ("0".equals(bean.getStatus()) || "1".equals(bean.getStatus())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "无法变更为注销或正常状态", void.class);
			}
			if (bean.getSplitMeterFlag() == 1 && StringUtils.isEmpty(bean.getSplitMeterWay())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "拆表类型不能未空", void.class);
			}
			// 获取停用信息
			bean.setCreateInfo(userInfo);
			bean.setBillNo(Tools.getBillNo(factory, "USER_STOP"));
			bean.setBillDate(Tools.getCurrentDate());
			// 记录停用信息
			mapper.userStopAdd(bean);
			//设置用户变更事项。只显示变更用户状态
			List<UserChangeItemBean> item = new ArrayList<UserChangeItemBean>();
			UserChangeItemBean ubean = new UserChangeItemBean();
			ubean.setReceiptId(bean.getId());
			ubean.setReceiptType("USER_STOP");
			ubean.setUserNo(bean.getUserNo());
			ubean.setBillNo(bean.getBillNo());
			ubean.setCreateInfo(userInfo);
			ubean.setChangeItem("用户状态");
			ubean.setOldItemValue("1");
			ubean.setNewItemValue(bean.getStatus());
			item.add(ubean);
			ChangeUserMapper cmapper = factory.getMapper(ChangeUserMapper.class);
			cmapper.userChangeItemAdd(item, 2);
			
			// 返回ID
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", bean.getId());
			msg.setData(map);
			BaseReceiptAccessoryMapper accessoryMapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
			if(!StringUtils.isBlank(bean.getTempId()))		//修改附件ID
				accessoryMapper.updateAccessoryReceiptId(bean.getId(), bean.getTempId());
			// 输出执行结果
			return msg;
		} catch (Exception e) {
			logger.error("停用增加失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "停用增加失败", void.class);
		}
	}

}
